<?php

namespace App\Model;

use Illuminate\Support\Carbon;
use Illuminate\Database\Query\Builder;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * App\Model\SaMenu.
 *
 * @property int         $id
 * @property int         $parent_id  父id
 * @property string      $name       名称
 * @property string|null $alias      别名
 * @property string|null $icon       菜单图标
 * @property int         $sort       排序，按照降序
 * @property int         $status     状态：0禁用 1启用
 * @property Carbon|null $created_at 创建时间
 * @property Carbon|null $updated_at 更新时间
 * @property Carbon|null $deleted_at 删除时间
 *
 * @method static \Illuminate\Database\Eloquent\Builder|SaMenu newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|SaMenu newQuery()
 * @method static Builder|SaMenu onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|SaMenu query()
 * @method static \Illuminate\Database\Eloquent\Builder|SaMenu whereAlias($value)
 * @method static \Illuminate\Database\Eloquent\Builder|SaMenu whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|SaMenu whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|SaMenu whereIcon($value)
 * @method static \Illuminate\Database\Eloquent\Builder|SaMenu whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|SaMenu whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|SaMenu whereParentId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|SaMenu whereSort($value)
 * @method static \Illuminate\Database\Eloquent\Builder|SaMenu whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|SaMenu whereUpdatedAt($value)
 * @method static Builder|SaMenu withTrashed()
 * @method static Builder|SaMenu withoutTrashed()
 * @mixin \Eloquent
 */
class SaMenu extends BaseModel
{
    use SoftDeletes;
    protected $table = 'sa_menu';

    /**
     * 获取菜单列表.
     *
     * @param $param
     *
     * @return mixed
     */
    public static function getMenus($param)
    {
        $where = [];
        if (array_key_exists('status', $param) && null != $param['status']) {
            $where[] = ['status', '=', (int) $param['status']];
        }
        if (array_key_exists('parent_id', $param) && null != $param['parent_id']) {
            $where[] = ['parent_id', '=', (int) $param['parent_id']];
        }

        $list = self::where($where)->orderBy('sort')->get()->toArray();

        return $list;
    }

    /**
     * 获取菜单信息.
     *
     * @param $param
     *
     * @return array
     */
    public static function getInfo($param)
    {
        $where = [];
        if (array_key_exists('id', $param) && !empty($param['id'])) {
            $where[] = ['id', '=', $param['id']];
        }

        $info  = self::where($where)->first();
        if ($info) {
            return $info->toArray();
        }

        return [];
    }

    /**
     * 添加菜单.
     *
     * @param $info
     *
     * @return mixed
     */
    public static function addMenu($info)
    {
        return self::insertGetId($info);
    }

    /**
     * 编辑菜单.
     *
     * @param $id
     * @param $info
     *
     * @return mixed
     */
    public static function editMenu($id, $info)
    {
        return self::where(['id' => $id])->update($info);
    }

    /**
     * 获取菜单数量.
     *
     * @param array $param
     *
     * @return mixed
     */
    public static function getCount($param = [])
    {
        $where = [];
        if (array_key_exists('parent_id', $param)) {
            $where[] = ['parent_id', '=', $param['parent_id']];
        }

        return self::where($where)->count();
    }

    /**
     * 删除菜单.
     *
     * @param $id
     *
     * @return mixed
     */
    public static function delMenu($id)
    {
        return self::where(['id' => $id])->delete();
    }
}
